/*
 * ScrollableWindow.cpp
 *
 *  Created on: 2012-11-9
 *      Author: yangentao@gmail.com
 */

#include "ScrollableWindow.h"

ScrollableWindow::ScrollableWindow() {
}

ScrollableWindow::~ScrollableWindow() {
}

void ScrollableWindow::setHScrollPos(int pos) {
	hScroll.pos = pos;

}
void ScrollableWindow::setHScrollMinMax(int minPos, int maxPos) {
	assert(minPos <= maxPos);
	hScroll.minPos = minPos;
	hScroll.maxPos = maxPos;
}
void ScrollableWindow::setHScrollPageSize(int pageSize) {
	hScroll.pageSize = pageSize;
}
void ScrollableWindow::setVScrollPos(int pos) {
	vScroll.pos = pos;
}
void ScrollableWindow::setVScrollMinMax(int minPos, int maxPos) {
	assert(minPos <= maxPos);
	vScroll.minPos = minPos;
	vScroll.maxPos = maxPos;
}
void ScrollableWindow::setVScrollPageSize(int pageSize) {
	vScroll.pageSize = pageSize;
}
void ScrollableWindow::updateHScroll(bool disableWhenUnnecessary) {
	SCROLLINFO si = { 0 };
	si.cbSize = sizeof(si);
	si.fMask = SIF_ALL;
	GetScrollInfo(getWnd(), SB_HORZ, &si);
	si.fMask = 0;
	if (si.nPos != hScroll.pos) {
		si.fMask |= SIF_POS;
		si.nPos = hScroll.pos;
	}
	if (si.nMin != hScroll.minPos || si.nMax != hScroll.maxPos) {
		si.fMask |= SIF_RANGE;
		si.nMin = hScroll.minPos;
		si.nMax = hScroll.maxPos;
	}
	if (si.nPage != hScroll.pageSize) {
		si.fMask |= SIF_PAGE;
		si.nPage = hScroll.pageSize;
	}
	if (si.fMask) {
		if (disableWhenUnnecessary) {
			si.fMask |= SIF_DISABLENOSCROLL;
		}
		SetScrollInfo(getWnd(), SB_HORZ, &si, TRUE);
	}

}
void ScrollableWindow::updateVScroll(bool disableWhenUnnecessary) {
	SCROLLINFO si = { 0 };
	si.cbSize = sizeof(si);
	si.fMask = SIF_ALL;
	GetScrollInfo(getWnd(), SB_VERT, &si);
	si.fMask = 0;
	if (si.nPos != vScroll.pos) {
		si.fMask |= SIF_POS;
		si.nPos = vScroll.pos;
	}
	if (si.nMin != vScroll.minPos || si.nMax != vScroll.maxPos) {
		si.fMask |= SIF_RANGE;
		si.nMin = vScroll.minPos;
		si.nMax = vScroll.maxPos;
	}
	if (si.nPage != vScroll.pageSize) {
		si.fMask |= SIF_PAGE;
		si.nPage = vScroll.pageSize;
	}
	if (si.fMask) {
		if (disableWhenUnnecessary) {
			si.fMask |= SIF_DISABLENOSCROLL;
		}
		SetScrollInfo(getWnd(), SB_VERT, &si, TRUE);
	}
}
void ScrollableWindow::updateScrolls(bool disableWhenUnnecessary) {
	updateHScroll(disableWhenUnnecessary);
	updateVScroll(disableWhenUnnecessary);
}

ScrollInfo ScrollableWindow::getHScrollInfo() {
	SCROLLINFO si = { 0 };
	si.cbSize = sizeof(si);
	si.fMask = SIF_ALL;
	GetScrollInfo(getWnd(), SB_HORZ, &si);
	ScrollInfo sp;
	sp.pos = si.nPos;
	sp.minPos = si.nMin;
	sp.maxPos = si.nMax;
	sp.pageSize = si.nPage;
	sp.trackPos = si.nTrackPos;
	return sp;
}
ScrollInfo ScrollableWindow::getVScrollInfo() {
	SCROLLINFO si = { 0 };
	si.cbSize = sizeof(si);
	si.fMask = SIF_ALL;
	GetScrollInfo(getWnd(), SB_VERT, &si);
	ScrollInfo sp;
	sp.pos = si.nPos;
	sp.minPos = si.nMin;
	sp.maxPos = si.nMax;
	sp.pageSize = si.nPage;
	sp.trackPos = si.nTrackPos;
	return sp;
}

void ScrollableWindow::showHScrollBar(bool show) {
	ShowScrollBar(getWnd(), SB_HORZ, show);
}
void ScrollableWindow::showVScrollBar(bool show) {
	ShowScrollBar(getWnd(), SB_VERT, show);
}
void ScrollableWindow::showScrollBars(bool show) {
	ShowScrollBar(getWnd(), SB_BOTH, show);
}
